<template>
  <div>
    <el-dialog v-model="addinfo.flag" :title="addinfo.isShow ? '添加菜单' : '编辑菜单'" center>
      <el-form :model="menuForm" :rules="rules" ref="formRef">
        <el-form-item label="所属角色" prop="roleid" :label-width="formLabelWidth">
          <el-select v-model="menuForm.roleid" placeholder="请选择">
            <el-option v-for="item in store.getters['role/getrole']" :key="item.id" :label="item.rolename"
              :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="管理员名称" prop="username" :label-width="formLabelWidth">
          <el-input autocomplete="off" v-model="menuForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password" :label-width="formLabelWidth">
          <el-input autocomplete="off" v-model="menuForm.password"></el-input>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch v-model="menuForm.status" style="
                                    --el-switch-on-color: #13ce66;
                                    --el-switch-off-color: #ff4949;
                                  " :active-value="1" :inactive-value="2" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="cancel">取消</el-button>
          <el-button @click="confirm"> 确认 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref, defineProps, defineEmits, defineExpose } from "vue";
import type { FormInstance, FormRules } from "element-plus";
import { ElMessage } from 'element-plus'
import { getuseradd, getuseredit, getuserinfo } from '../../request/api'
import { useStore } from 'vuex'
let store = useStore()

//接收父组件的属性
let props = defineProps({
  addinfo: Object,
});
let emit = defineEmits(["cancel"]);
//label宽度
let formLabelWidth = ref("120px");
let formRef = ref<FormInstance>();
//创建一个表单对象
let menuForm = ref({
  roleid: '',
  username: '',
  password: '',
  status: 1
});
//规则验证
let rules = reactive<FormRules>({
  username: [
    { required: true, message: "请选择管理名称", trigger: "blur" },
    { min: 1, max: 6, message: "Length should be 2 to 6", trigger: "blur" },
  ],
  pid: [
    {
      required: true,
      message: "Please select pid",
      trigger: "change",
    },
  ],
});

//页面一打开判断role列表没有的话在调用role函数
let mounted = () => {
  if (store.getters['role/getrole'].length == 0) {
    store.dispatch('role/getroleActive')
  }
}
mounted()
//封装重置事件
let reset = () => {
  menuForm.value = ref({
    roleid: '',
    username: '',
    password: '',
    status: 1
  });
  //清空验证规则
  formRef.value.resetFields()

};
//封装一个取消事件
let cancel = () => {
  emit("cancel", false);
  reset();
};

//封装一个获取一条数据的方法
let lookup = (uid: any) => {
  console.log(uid);
  getuserinfo({ uid }).then((res: any) => {
    menuForm.value = res.list
    menuForm.value.uid = uid
    menuForm.value.password = ''
  })
}
defineExpose({
  lookup,
});

//封装一个确定事件
const confirm = () => {
  formRef.value?.validate((val) => {
    if (val) {
      if (props.addinfo.isShow) {
        getuseradd(menuForm.value).then((res: any) => {
          if (res.code == 200) {
            ElMessage.success(res.msg)
            store.dispatch('admin/getadminAction')
            store.dispatch('admin/getCountAction')
            cancel()
          } else {
            ElMessage.error(res.msg)
          }
        })
      } else {
        //编辑接口
        getuseredit(menuForm.value).then((res: any) => {
          if (res.code == 200) {
            ElMessage.success(res.msg)
            store.dispatch('admin/getadminAction')
            store.dispatch('admin/getCountAction')
            cancel()
          } else {
            ElMessage.error(res.msg)
          }
        })
      }

    }
  });
};
</script>

<style lang="less" scoped></style>
